package java学习;

import java.security.CryptoPrimitive;
import java.util.*;

/**
 * **************
 * 项目名称: 蓝桥杯 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/3/2 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class TestHashSet {

    public static void main(String[] args) {
//        HashSet set = new HashSet();
//        set.add("lucy");//添加成功
//        set.add("lucy");//加入不了
//        set.add(new Dog("tom"));//OK
//        set.add(new Dog("tom"));//Ok
//        set.add(new String("hsp"));//ok
//        set.add(new String("hsp"));//加入不了
//        System.out.println(set);

       Set linkedHashSet = new LinkedHashSet();
       linkedHashSet.add(new String("AA"));
       linkedHashSet.add(456);
       linkedHashSet.add(456);
       linkedHashSet.add(new Dog("刘"));
       linkedHashSet.add(123);
       linkedHashSet.add("HSP");
        linkedHashSet.add(new Dog("shij"));
        linkedHashSet.add("45666");

        System.out.println(linkedHashSet);

        Map<Dog,String> map = new HashMap<Dog,String>();
//        map.put("邓超", "孙俪"); map.put("王宝强", "马蓉"); map.put("宋喆", "马蓉"); map.put("刘令博", null); map.put(null, "刘亦菲"); map.put("鹿晗", "关晓彤");
        for (int i = 0; i <=12 ; i++) {
            map.put(new Dog(i+""), "hello");
        }

        Set<Dog> dogs = map.keySet();
        for (Dog o : dogs) {
            String s = map.get(o);
        }
        Set<Map.Entry<Dog, String>> entries = map.entrySet();
        for (Map.Entry<Dog, String> entry : entries) {
            entry.getValue();
        }
        Collection<String> values1 = map.values();
        System.out.println(map);

        System.out.println("第一种《根据key集合》");

        Set keySet = map.keySet(); // 获取到 key集合
        for (Object o :keySet){
            Object o1 = map.get(o);
            System.out.println(o+"--"+o1);

        }
        System.out.println("第一种1迭代器《》");
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next+"---"+map.get(next));

        }

        System.out.println("第二种 entrySet获取 k-v");
        Set set = map.entrySet();
        for (Object o : set) {
            Map.Entry entry = (Map.Entry) o;// 每个元素是 HashMap$Node 转成 Entry是因为Entry有getKey，getValue方法
            System.out.println( entry.getKey()+"--"+ entry.getValue());
        }

        System.out.println("第三种：《根据value集合》");
        Collection values = map.values();
        for (Object o : values){
            System.out.println(o);
        }

        Hashtable<Object, Object> objectObjectHashtable = new Hashtable<>();
//        objectObjectHashtable.put(null,12);
        TreeSet<Object> treeSet = new TreeSet<>(new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((String)o1).length() - ((String)o2).length();
            }
        });
        treeSet.add("123");
        treeSet.add("456");
        System.out.println(treeSet);


        HashSet set1 = new HashSet();//ok
        Cat p1 = new Cat(1001,"AA");
        Cat p2 = new Cat(1002,"BB");
        set1.add(p1); set1.add(p2);
        p1.Cname="CC"; // 此时将Cname 改动，则hash不一样了，故无法删除p1
        set1.remove(p1);
        System.out.println(set1);

        set1.add(new Cat(1001,"CC"));
        System.out.println(set1);

        set1.add(new Cat(1001,"AA"));
        System.out.println(set1);

//        Object  s = new String();
//       String a = (String) new Object();
    }




}
class Dog {
    String name ;
    public Dog(String name) {
        this.name = name;
    }
    @Override
    public int hashCode() {
        return 101;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                '}';
    }
}
